/************************************************************************/
// Copyright (C) 2016, han_gangbiao. All rights reserved.
// Use of this source code is governed by a GPL-v2 license that can be found in the License file.
// 
// [Time]:      2016-3-20 16:43:50
// [Author]:    han_gangbiao [http://my.oschina.net/jackhen]
// [Info]:      
/************************************************************************/

#ifndef INCLUDE_EXAMPLES_RPC_SERVER_H_
#define INCLUDE_EXAMPLES_RPC_SERVER_H_

#include "base/xsingleton.h"
#include "base/xthread.h"
#include "base/xmutex.h"
#include "base/xguard.h"
#include "rpc/rpc_client_base.h"
#include "rpc/RpcClientMgr.h"
#include <string>

class CRPCClient : public CRpcClientBase
{
public:
    CRPCClient( const std::string& remote_ip, int remote_port, CRpcClientMgr* pMgr );
private:
    virtual ~CRPCClient();

public:
    int start_agent();
    int stop_agent();
    bool isOffline();
protected:
    virtual int on_invoke( smart::ref_ptr<NetBinaryPdu> pdu );
    virtual int on_rpc_close( int errcode );
    
private:
    bool m_bOffline;
    smart::mutex_t m_lock;

    std::string m_remote_ip;
    int m_remote_port;
};


class CAutoReconnSingleton : public smart::singleton_t<CAutoReconnSingleton>, public smart::threadbase_t
{
    friend class smart::singleton_t<CAutoReconnSingleton>;
public:
    int start_run( const std::string& remote_ip, int remote_port, CRpcClientMgr* pMgr );
    int stop_run();

protected:
    virtual int run();
private:
    CAutoReconnSingleton();
    virtual ~CAutoReconnSingleton();

private:
    smart::ref_ptr<CRPCClient> m_pClient;
};

#endif //~INCLUDE_EXAMPLES_RPC_SERVER_H_

